package com.chushouya.manager.service.api.impl;


import com.general.framework.core.lang.*;
import com.chushouya.common.support.Contexts;
import com.general.framework.data.redis.RedisCache;
import com.chushouya.manager.dao.entity.AppConfigListEntity;
import com.chushouya.manager.dao.repository.AppConfigListRepository;
import com.chushouya.manager.dto.admin.appconfiglist.AppConfigDTO;
import com.chushouya.manager.dto.admin.appconfiglist.AppConfigQuery;
import com.chushouya.manager.service.api.AppConfigListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class AppConfigListServiceImpl implements AppConfigListService {


    @Resource
    private AppConfigListRepository appConfigListRepository;

    @Resource
    private RedisCache redisCache;

    /**
     * app设置页
     */
    private static final String APP_CONFIG_LIST = "app_config_list:{}";

    /**
     * 获取app用户页
     */
    private static final String APP_USER_CONFIG_LIST = "app_user_config_list:{}";

    @Override
    public List<AppConfigDTO> appConfigList() {
        final String appId = Contexts.getHeaderContext().getAppId();
        return this.getAppConfigCacheList(appId);
    }

    @Override
    public List<AppConfigDTO> userConfigList() {
        final Long partnerId = Contexts.getHeaderContext().getPartnerId();
        return this.getUserConfigCacheList(partnerId);
    }

    /**
     * 获取设置页缓存列表
     */
    public List<AppConfigDTO> getAppConfigCacheList(String appId) {
        final String cacheKey = Strings.format(APP_CONFIG_LIST, appId);
        if (redisCache.exist(cacheKey)) {
            return redisCache.getCacheObject(cacheKey);
        }
        AppConfigQuery query = new AppConfigQuery();
        query.setAppId(appId);
        List<AppConfigDTO> appConfigList = Beans.copyList(appConfigListRepository.selectList(query), AppConfigDTO.class);
        redisCache.setCacheObject(cacheKey, appConfigList, Numbers.LONG_ONE, TimeUnit.DAYS);
        return appConfigList;
    }

    /**
     * 获取用户设置页缓存列表
     */
    public List<AppConfigDTO> getUserConfigCacheList(Long partnerId) {
        final String cacheKey = Strings.format(APP_USER_CONFIG_LIST, partnerId);
        if (redisCache.exist(cacheKey)) {
            return redisCache.getCacheObject(cacheKey);
        }
        AppConfigQuery query = new AppConfigQuery();
        query.setAppId(Strings.EMPTY);
        List<AppConfigListEntity> appConfigEntityList = appConfigListRepository.selectList(query);
        List<AppConfigDTO> appConfigList = Lists.newArrayList();
        appConfigEntityList.forEach(e -> {
            final List<String> appList = Jsons.parseArray(e.getAppList(), String.class);
            if (appList.contains(partnerId)) {
                appConfigList.add(Beans.copy(e, AppConfigDTO.class));
            }
        });
        redisCache.setCacheObject(cacheKey, appConfigList, Numbers.LONG_ONE, TimeUnit.DAYS);
        return appConfigList;
    }
}
